package com.demomaster.quickserialport;

import android.util.Log;

public class ByteUtil {
    //-------------------------------------------------------
    // 判断奇数或偶数，位运算，最后一位是1则为奇数，为0是偶数
    public static int isOdd(int num) {
        return num & 1;
    }

    //-------------------------------------------------------
    //Hex字符串转int
    public static int HexToInt(String inHex) {
        Log.e("==sizeS",inHex);
        return Integer.parseInt(inHex, 16);
    }

    /**
     * 字符串转化为byte数据
     */
    public static byte[] strTobyte(String data) {
        data = data.toUpperCase();
        int len = data.length() / 2;
        byte[] sendData = new byte[len];
        char[] hc = data.toCharArray();
        for (int i = 0; i < len; i++) {
            int p = 2 * i;
            sendData[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p + 1]));
        }
        return sendData;
    }

    /**
     * 字符转换为字节
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static String ByteArrayToString(byte[] bt_ary) {

        StringBuilder sb = new StringBuilder();
        if (bt_ary != null)
            for (byte b : bt_ary) {
                sb.append(String.format("%02X ", b));
            }
        return sb.toString();
    }

    //-------------------------------------------------------
    //Hex字符串转byte
    public static byte HexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }

    //-------------------------------------------------------
    //1字节转2个Hex字符
    public static String Byte2Hex(byte inByte) {
        return String.format("%02x", inByte).toUpperCase();
    }

    //-------------------------------------------------------
    //字节数组转转hex字符串
    public static String ByteArrToHex(byte[] inBytArr) {
        StringBuilder strBuilder = new StringBuilder();
        for (byte byt : inBytArr) {
            strBuilder.append(Byte2Hex(byt));
            strBuilder.append(" ");
        }
        inBytArr = null;
        return strBuilder.toString();
    }

    //-------------------------------------------------------
    //字节数组转转hex字符串
    public static String ByteArrToHexALL(byte[] inBytArr) {
        StringBuilder strBuilder = new StringBuilder();
        for (byte byt : inBytArr) {
            strBuilder.append(Byte2Hex(byt));

        }
        return strBuilder.toString();
    }

    //-------------------------------------------------------
    //字节数组转转hex字符串，可选长度
    public static String ByteArrToHex(byte[] inBytArr, int offset, int byteCount) {
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < byteCount; i++) {
            strBuilder.append(Byte2Hex(inBytArr[offset + i]));
        }
        return strBuilder.toString();
    }

    //-------------------------------------------------------
    //把hex字符串转字节数组
    public static byte[] HexToByteArr(String inHex) {
        byte[] result;
        int hexlen = inHex.length();
        if (isOdd(hexlen) == 1) {
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = HexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }
    /**
     * 截取byte数组   不改变原数组
     *
     * @param b      原数组索引
     * @param off    偏差值（）
     * @param length 长度
     * @return 截取后的数组
     */
    public static byte[] subByte(byte[] b, int off, int length) {
        byte[] b1 = new byte[length];
        System.arraycopy(b, off, b1, 0, length);
        return b1;
    }
    /**
     * byte[]转int
     * @param bytes
     * @return
     */
    public static int byteArrayToInt(byte[] bytes) {
        int value = 0;
        int n = Math.min(bytes.length, 4);
        for (int i = 0; i < n; i++) {
            int shift = (n - 1 - i) * 8;
            value += (bytes[i] & 0xFF) << shift;
        }
        return value;
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X ", b));
        }
        return sb.toString().trim();
    }

    public static byte[] byteMerger(byte[] bt1, byte[] bt2) {
        byte[] bt3 = new byte[bt1.length + bt2.length];
        System.arraycopy(bt1, 0, bt3, 0, bt1.length);
        System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
        return bt3;
    }


    /**
     * 判定字节数组是否以指定数组开头+指定数组结尾
     */
    public boolean IsMatch(byte[] src, byte[] begin, byte[] end) {
        if (src == null) return false;
        int lenSrc = src.length;
        if (begin != null) {
            if (lenSrc < begin.length) return false;
            for (int i = 0; i < begin.length; i++) {
                if (src[i] != begin[i]) return false;
            }
        }
        if (end != null) {
            if (lenSrc < end.length) return false;
            for (int i = 1; i < end.length - 1; i++) {
                if (src[lenSrc - i] != end[end.length - i]) return false;
            }
        }
        return true;
    }
}
